//
//  YWFaceController.m
//  YWPhotoEditMaster
//
//  Created by jun peng on 2023/8/3.
//

#import "YWFaceController.h"

#import "YWFaceView.h"
@interface YWFaceController ()

- (IBAction)dismissClick:(UIButton *)sender;
- (IBAction)conformBtnClick:(UIButton *)sender;


@property (weak, nonatomic) IBOutlet UIView *handleView;

@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView; 

@property (weak, nonatomic) YWFaceView *drawView;

@property(nonatomic,assign)BOOL isAlreadyAppear;


@end

@implementation YWFaceController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    //给imageView原图
    self.imageView.image = self.image;
    
    
    //创建画view并添加到bgView
    YWFaceView *drawView = [[YWFaceView alloc] init];
    self.drawView = drawView;
    [self.bgView addSubview:drawView];
    
}


//初始化UI
-(void)setupUI{
    
    self.view.alpha = 0;
    self.handleView.transform = CGAffineTransformMakeTranslation(0, 300);
    
    
    NSArray *imageArray = @[@"贝斯",@"檫",@"大号",@"电琴",@"二胡",@"钢琴",@"架子鼓",@"平鼓"];
    
    for (NSInteger i = 0; i < imageArray.count; i++) {
        NSString *imageStr = imageArray[i];
        UIButton *btn = [[UIButton alloc] init];
        [btn setImage:[UIImage imageNamed:imageStr] forState:UIControlStateNormal];
        btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        btn.tag = i;
        [self.handleView addSubview:btn];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = [UIColor colorWithWhite:0.98 alpha:1];
    }
    
}


-(void)btnClick:(UIButton *)sender{
    
    [self.drawView setFaceImage:sender.imageView.image];
    
}


-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    CGFloat margin = 10;
    NSInteger shu = 4;
    NSInteger heng = 2;
    CGFloat w = (self.handleView.frame.size.width - margin * (shu + 1))*1.0/shu;
    CGFloat h = (self.handleView.frame.size.height - margin * (heng + 1))*1.0/heng;
    for (NSInteger i = 0; i < self.handleView.subviews.count; i++) {
        
        UIButton *btn = self.handleView.subviews[i];
        CGFloat x = (i % shu) * (w + margin) + margin;
        CGFloat y = (i / shu) * (h + margin) + margin;
        btn.frame = CGRectMake(x, y, w, h);
        
        
    }
    
    
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [UIView animateWithDuration:0.4 animations:^{
        
        self.view.alpha = 1;
        
        self.handleView.transform = CGAffineTransformIdentity;
    }];
    
    
    //显示第一张底图层
    if (self.isAlreadyAppear == NO){
        
        self.isAlreadyAppear = YES;
        
        self.imageView.hidden = YES; //不是对比的话一开始imageView就要隐藏
        
        MJWeakSelf
        
        CGFloat w = self.bgView.frame.size.width;
        CGFloat h = w * self.image.size.height*1.0/self.image.size.width;
        CGFloat y = 0;
        CGFloat x = 0;
        if (h > self.bgView.frame.size.height) {
            h = self.bgView.frame.size.height;
            y = 0;
            w = h * self.image.size.width*1.0/self.image.size.height;
            x = (self.bgView.frame.size.width - w)*1.0/2;
            
            [self.drawView mas_makeConstraints:^(MASConstraintMaker *make) {
               make.left.equalTo(weakSelf.drawView.superview).with.offset(x);
               make.right.equalTo(weakSelf.drawView.superview).with.offset(-x);
               make.bottom.equalTo(weakSelf.drawView.superview).with.offset(0);
               make.top.equalTo(weakSelf.drawView.superview).with.offset(0);
            }];
            
        }
        else{
            x = 0;
            w = self.bgView.frame.size.width;
            h = w * self.image.size.height*1.0/self.image.size.width;
            y = (self.bgView.frame.size.height - h)*0.5;
            
            
            [self.drawView mas_makeConstraints:^(MASConstraintMaker *make) {
               make.left.equalTo(weakSelf.drawView.superview).with.offset(0);
               make.right.equalTo(weakSelf.drawView.superview).with.offset(0);
                make.top.equalTo(weakSelf.drawView.superview).with.offset(y);
               make.bottom.equalTo(weakSelf.drawView.superview).with.offset(-y);
            }];
            
        }
        
        //设置第一张原始图片
        [self.drawView setFirstImage:self.image];
    }
    
    
    
}

-(void)dismiss{
    [UIView animateWithDuration:0.4 animations:^{
        
        self.handleView.transform = CGAffineTransformMakeTranslation(0, 300);
        self.view.alpha = 0;
    }completion:^(BOOL finished) {
        
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}

- (IBAction)dismissClick:(UIButton *)sender {
    [self dismiss];
}

- (IBAction)conformBtnClick:(UIButton *)sender {
    
    if([self.delegate respondsToSelector:@selector(faceController:newImage:)]){
        [self.delegate faceController:self newImage:[self.drawView getCurrentLayer]];
    }
    
    [self dismiss];
    
}



@end
